package managers.messengerManager.services;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ResourceBundle;

import managers.conversationManager.ConversationManager;
import managers.messengerManager.FeedbackManager;
import managers.messengerManager.MessengerInitializingException;
import managers.messengerManager.MessengerManager;
import utils.ManagingMessage;
import utils.ManagingMessage.Type;
import utils.StackTracePrinter;
import utils.UserData;

public class ConnectionService extends Service {

	private static final int DEFAULT_CONNECTION_ATTEMPTS = 3;

	private ConversationManager convManager = ConversationManager.getSharedInstance();
	
	private int connectionAttempts;
	private int baseWaitTime;

	public ConnectionService () throws UnknownHostException {
		this (DEFAULT_CONNECTION_ATTEMPTS, DEFAULT_BASE_WAIT_TIME);
	}

	public ConnectionService (int connectionAttempts, int baseWaitTime) throws UnknownHostException {
		super("Connection", Thread.MAX_PRIORITY);

		this.connectionAttempts = connectionAttempts;
		this.baseWaitTime = baseWaitTime;

	}


	@Override
	public void run() {
		try {
			manager.getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.6"), FeedbackManager.WARNING);
			manager.getFeedbackManager().enableUserInput(false);
			requestConnection();
			manager.getFeedbackManager().updateStatus(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.7"), FeedbackManager.DEFAULT);
			manager.getFeedbackManager().enableUserInput(true);
		} catch (Exception e) {
			StackTracePrinter.print(e);
			manager.getFeedbackManager().printErrorAndExit(ResourceBundle.getBundle("managers.messengerManager.services.messages").getString("m.8"));
			manager.getFeedbackManager().enableUserInput(true);
		}
	}


	public synchronized void requestConnection() throws Exception {
		UserData user = manager.getDataManager().getUser(MessengerManager.getSharedInstance().getUserId());
		user.putProperty("ipAddress", InetAddress.getLocalHost());
		user.putProperty("port", manager.getSingleProtocol().getPort());
		user.putProperty("fileTransferPort", convManager.getProtocol().getFileTransferPort());
		user.putProperty("conversationPort", convManager.getProtocol().getConversationPort());
		user.putProperty("fileTransferPort", convManager.getProtocol().getFileTransferPort());

		ManagingMessage request = new ManagingMessage(Type.LOGIN_REQUEST);
		request.putProperty("userId", manager.getUserId());
		request.putProperty("conversationPort", convManager.getProtocol().getConversationPort());
		request.putProperty("fileTransferPort", convManager.getProtocol().getFileTransferPort());
		int i = 0;
		while (i<connectionAttempts) {
			manager.getSingleProtocol().send(request, manager.getServerAddress(), manager.getServerPort());
			wait(baseWaitTime*(i+1));
			if (externalObject  != null) {
				externalObject = null;
				return;
			}
			else {
				i++;
			}
		}
		throw new MessengerInitializingException("Server do not respond.");		
	}

}
